#!/bin/sh
# Startup script for nfs-utils (simplified)
NFS_SERVERS=8
NFS_MOUNTD=/usr/sbin/rpc.mountd
NFS_NFSD=/usr/sbin/rpc.nfsd
. /etc/init.d/functions
test -r /etc/default/nfsd && . /etc/default/nfsd

# Startup and shutdown functions.
start_mountd(){
	echo -n 'starting mountd: '
	start-stop-daemon --start --exec "$NFS_MOUNTD" -- "$@"
	echo done
}

stop_mountd(){
	echo -n 'stopping mountd: '
	start-stop-daemon --stop --quiet --exec "$NFS_MOUNTD"
	echo done
}

start_nfsd(){
	modprobe -q nfsd
	set -e
	grep -q nfsd /proc/mounts || mount -t nfsd nfsd /proc/fs/nfsd
	echo -n "starting $1 nfsd kernel threads: "
	start-stop-daemon --start --exec "$NFS_NFSD" -- "$@"
	echo done
}

delay_nfsd(){
	for delay in 0 1 2 3 4 5 6 7 8 9 
	do
		if pidof nfsd >/dev/null
		then
			echo -n .
			sleep 1
		else
			return 0
		fi
	done
	return 1
}

stop_nfsd(){
	# WARNING: this kills any process with the executable
	# name 'nfsd'.
	echo -n 'stopping nfsd: '
	start-stop-daemon --stop --quiet --signal 1 --name nfsd
	if delay_nfsd || {
		echo failed
		echo ' using signal 9: '
		start-stop-daemon --stop --quiet --signal 9 --name nfsd
		delay_nfsd
	}
	then
		echo done
	else
		echo failed
	fi
}

case "$1" in
	start)
		start_nfsd "$NFS_SERVERS"
		start_mountd
		test -r /etc/exports || echo "/media/hdd	*(rw,no_root_squash,sync)" > /etc/exports
		exportfs -a
		;;
	stop)
		exportfs -ua
		stop_mountd
		stop_nfsd
		;;
	status)
		status /usr/sbin/rpc.mountd
		RETVAL=$?
		status nfsd
		rval=$?
		[ $RETVAL -eq 0 ] && exit $rval
		exit $RETVAL
		;;
	reload)
		test -r /etc/exports && exportfs -r
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|status|reload|restart}"
		exit 1
		;;
esac
